package com.quickask.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by hackway on
 * Date: 2015/12/22.
 * Time:15:09
 */
public class SessionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("========  session filter start!  ========");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String token = servletRequest.getParameter("token");
        String uid = servletRequest.getParameter("userId");
        HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
        HttpSession session = httpRequest.getSession();
        System.out.println("parameter----->>>>> "+uid+": "+token);
        System.out.println("token------------>>>> "+session.getAttribute(uid));
//        System.out.println(uid+": "+token);
//        if (session.getAttribute(uid)==null || !session.getAttribute(uid).equals(token)) {
//            servletResponse.getWriter().write("Error");
//            servletResponse.getWriter().close();
//        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("========  session filter destroy!  ========");
    }
}
